package cn.itcast.servlet;

import cn.itcast.service.CustomerService;
import cn.itcast.vo.Customer;
import cn.itcast.vo.PageBean;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ListByPageServlet
  extends HttpServlet
{
  private static final long serialVersionUID = 2372947267730388684L;
  
  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    int pageCode = getPageCode(request);
    int pageSize = 10;
    
    CustomerService cs = new CustomerService();
    
    PageBean<Customer> page = cs.findByPage(pageCode, pageSize);
    
    request.setAttribute("page", page);
    request.getRequestDispatcher("/pages/listByPage.jsp").forward(request, response);
  }
  
  public int getPageCode(HttpServletRequest request)
  {
    String pc = request.getParameter("pc");
    if ((pc == null) || (pc.trim().isEmpty())) {
      return 1;
    }
    return Integer.parseInt(pc);
  }
  
  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    doGet(request, response);
  }
}
